"""
(1) nginx日志转换生成json
 61.235.166.101 -- [1/Feb/2024:05:30:26 +0800] "POST /api/post/add HTTP/1.1" 200 410 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36 Edg/121.0.0.0"
{
    "ip":"61.235.166.101",
    "datetime":"1/Feb/2024:05:30:26",
    "method":"POST",
    "url":"/api/post/add",
    "protocal":"HTTP/1.1",
    "status_code":200,
    "size": 410,
    "user_agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) ..."
}

(2) 用户行为日志转换（扁平化）
例如：
{
    "page":{
        "x":10,
        "y":20
    },
    "common":{
        "area":"china"
    }
}
转换生成
{
    "page_x":10,
    "page_y":20,
    "common_area":"china"
}
(3) 数据库日志（maxwell）
{
    "table":"tableName",
    "operate":"update|insert",
    "old_data":...,
    "new_data":...,
    "timestamp":123123123
}
"""
import json
import re
import datetime


class LogETLUtil:
    @classmethod
    def nginx_log_tran(cls, txt) -> str:
        """
        nginx日志转换方法
        :param txt:
        :return:
        """
        f1 = re.findall(r'\".+?\"', txt)
        f2 = re.findall(r'\[(.+)]', txt)

        t1 = f1[0][1:-1].split(" ")
        t2 = txt.split(" ")
        res = {
            "ip": txt.split(" ")[0],
            "datetime": f2[0],
            "method": t1[0],
            "url": t1[1],
            "protocal": t1[2],
            "status_code": int(t2[8]),
            "size": int(t2[9]),
            "user_agent": f1[1][1:-1]
        }
        # res = re.findall(r'\".+?\"', txt)
        print(res)
        return ''

    @classmethod
    def user_event_log_tran(cls, txt) -> str:
        """
        用户行为日志转换方法
        :param txt:
        :return:
        """

        res = {}

        def getItemTxt(key, value):

            if isinstance(value, dict):
                for k in value:
                    getItemTxt(key + '_' + k, value[k])
            elif isinstance(value, list):
                for i in range(len(value)):
                    getItemTxt(key + "_" + str(i), value[i])
            else:
                res[key] = value

        j = json.loads(txt)
        for k in j:
            getItemTxt(k, j[k])

        return res

    @classmethod
    def db_log_tran(cls, txt) -> str:
        """
        数据库日志转换方法
        :param txt:
        :return:
        """
        j = json.loads(txt)
        res = {
            "database": j["database"],
            "table": j["table"],
            "operate": j["type"],
            "old_data": "" if j["type"]=='insert' else json.dumps(j["old"]),
            "new_data": json.dumps(j['data']),
            "timestamp": j["ts"]
        }
        return res


if __name__ == '__main__':
    # txt1 = '127.0.0.1 - - [14/Aug/2023:07:53:51 +0800] "POST /index.php/api/student HTTP/1.1" 200 121348 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36 Edg/121.0.0.0"'
    # r1 = LogETLUtil.nginx_log_tran(txt1)
    # print(r1)

    # txt2 = '{"common": {"ar": "0758", "ba": "三星", "ch": "渠道9", "is_new": "1", "md": "三星 型号4", "mid": "47", "os": "Harmony", "sid": "QjeY7biKvZ6QAkgiZjDa", "uid": "47", "vc": "v2.1.134"}, "actions": [{"action_id": "common_add", "item": 6698, "item_type": "course_id", "ts": 1711335177742}], "displays": [{"displayType": "activity", "item": 5304, "item_type": "course_id", "order": 1, "pos_id": 1}, {"displayType": "activity", "item": 7029, "item_type": "course_id", "order": 2, "pos_id": 2}, {"displayType": "promotion", "item": 7232, "item_type": "page_id", "order": 3, "pos_id": 3}], "page": {"during_time": 35240, "item": 6688, "item_type": "video_id", "last_page_id": "payment", "page_id": "course_detail", "from_pos_id": 131, "from_pos_seq": 652, "refer_id": 4, "sourceType": "来源13"}, "err": {"error_code": "0", "msg": "***********"}, "ts": 1711335177742}'
    # r2 = LogETLUtil.user_event_log_tran(txt2)
    # print(r2)

    # txt3 = '{"database":"test1","table":"test","type":"update","ts":1711936566,"xid":1631666,"commit":true,"data":{"id":6,"name":"446","value":"123123"},"old":{"value":"444"}}'
    # r3 = LogETLUtil.db_log_tran(txt3)
    # print(r3)

    pass